<?php

namespace App\Models\Plane\Rules;

use App\Trip\Plane\CarrierCode;
use App\Trip\Traits\ModelDateTrait;
use Eloquent;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

/**
 * App\Models\Plane\Rules\PlaneLuggageRules
 *
 * @property int         $id
 * @property string      $carrier_code 航司二字码
 * @property string      $company_name 航空公司名称
 * @property string      $cabin_name 舱位名称
 * @property string      $cabin_code 舱位code
 * @property string      $checked_weight 托运行李重量
 * @property string      $checked_volume 托运行李体积 长*宽*高
 * @property string      $checked_number 托运行李数量
 * @property string      $carry_weight 手提行李重量
 * @property string      $carry_volume 手提行李体积 长*宽*高
 * @property string      $carry_number 手提行李数量
 * @property string      $carry_single_weight 手提行李 单件重量
 * @property string      $baby_checked_weight 携带的婴儿乘客可托运的婴儿车重量
 * @property string      $baby_checked_volume 携带的婴儿乘客可托运的婴儿车体积
 * @property string      $baby_checked_number 携带的婴儿乘客可托运的婴儿车数量
 * @property string      $baby_desc 婴儿行李描述
 * @property string      $baby_desc_uy 婴儿行李描述维语
 * @property string      $remark 行李备注
 * @property string      $remark_uy 行李备注维语
 * @property int         $from  1 主要由 excel 导入  2 api
 * @property int         $type 区分方式 1 舱位 , 2 票价
 * @property string      $ticket_price_scopes 区分方式为票价:票价区间 一般 0,1 全价 ,0,0.5 5折以内,0.5,1 半价-全价
 * @property int         $default 默认 1 是  0 否
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @method static Builder|PlaneLuggageRule newModelQuery()
 * @method static Builder|PlaneLuggageRule newQuery()
 * @method static Builder|PlaneLuggageRule query()
 * @method static Builder|PlaneLuggageRule whereBabyCheckedNumber($value)
 * @method static Builder|PlaneLuggageRule whereBabyCheckedVolume($value)
 * @method static Builder|PlaneLuggageRule whereBabyCheckedWeight($value)
 * @method static Builder|PlaneLuggageRule whereBabyDesc($value)
 * @method static Builder|PlaneLuggageRule whereBabyDescUy($value)
 * @method static Builder|PlaneLuggageRule whereCabinCode($value)
 * @method static Builder|PlaneLuggageRule whereCabinName($value)
 * @method static Builder|PlaneLuggageRule whereCarrierCode($value)
 * @method static Builder|PlaneLuggageRule whereCarryNumber($value)
 * @method static Builder|PlaneLuggageRule whereCarrySingleWeight($value)
 * @method static Builder|PlaneLuggageRule whereCarryVolume($value)
 * @method static Builder|PlaneLuggageRule whereCarryWeight($value)
 * @method static Builder|PlaneLuggageRule whereCheckedNumber($value)
 * @method static Builder|PlaneLuggageRule whereCheckedVolume($value)
 * @method static Builder|PlaneLuggageRule whereCheckedWeight($value)
 * @method static Builder|PlaneLuggageRule whereCompanyName($value)
 * @method static Builder|PlaneLuggageRule whereCreatedAt($value)
 * @method static Builder|PlaneLuggageRule whereDefault($value)
 * @method static Builder|PlaneLuggageRule whereFrom($value)
 * @method static Builder|PlaneLuggageRule whereId($value)
 * @method static Builder|PlaneLuggageRule whereRemark($value)
 * @method static Builder|PlaneLuggageRule whereRemarkUy($value)
 * @method static Builder|PlaneLuggageRule whereTicketPriceScopes($value)
 * @method static Builder|PlaneLuggageRule whereType($value)
 * @method static Builder|PlaneLuggageRule whereUpdatedAt($value)
 * @mixin Eloquent
 */
class PlaneLuggageRule extends Model
{
    use HasFactory, ModelDateTrait;

    protected $table = "plane_luggage_rules";
    protected $fillable
                     = [
            "carrier_code",
            "company_name",
            "cabin_name",
            "cabin_code",
            "checked_weight",
            "checked_volume",
            "checked_number",
            "carry_weight",
            "carry_volume",
            "carry_number",
            "carry_single_weight",
            "baby_checked_weight",
            "baby_checked_volume",
            "baby_checked_number",
            "baby_desc",
            "baby_desc_uy",
            "remark",
            "remark_uy",
            "from",               // 来自 1 excel 2 api
            "type",               // 区分方式 1 舱位 , 2 票价
            "default",            // 默认 1 是  0 否
            "ticket_price_scopes",// 区分方式为票价:票价区间 一般 0,1 全价 ,0,0.5 5折以内,0.5,1 半价-全价
        ];


    public function add(array $data)
    {
        if ($rule = $this->findByUnique($data)) {
            Log::error("行李规则已存在,{$data['type']},{$data['cabin_name']},{$data['cabin_code']}");
            return $rule;
        }
        return $this->create($data);
    }


    private function findByUnique($data)
    {
        return $this
            ->where('carrier_code', $data['carrier_code'])
            ->where('cabin_name', $data['cabin_name'])
            ->where('cabin_code', $data['cabin_code'])
            ->where('type', $data['type'])
            ->where('ticket_price_scopes', $data['ticket_price_scopes'])
            ->first();
    }

    /**
     * @throws Exception
     */
    public function findByCarrierCode(string $flight_no): Collection
    {
        $carrier_code = CarrierCode::getCarrierCode($flight_no);
        return $this->where('carrier_code', $carrier_code)
                    ->get();
    }

}
